<template>
  <div class="custom-tab-page">
    <div class="tab4-title">
      <div class="left">
        <el-input style="width: 180px" v-model="name" placeholder="合同名称" />
        <el-input
          v-model="customer_name"
          style="width: 180px; margin-left: 20px"
          placeholder="客户名称"
        ></el-input>
        <el-select
          v-model="order_status"
          clearable
          style="width: 180px; margin-left: 20px"
          placeholder="合同发货状态"
        >
          <el-option label="待发货" :value="3" />
          <el-option label="已发货" :value="4" />
        </el-select>

        <el-date-picker
          style="width: 180px; margin-left: 20px"
          v-model="startDate"
          type="date"
          placeholder="开始日期"
          value-format="YYYY-MM-DD"
        />
        <el-date-picker
          style="width: 180px; margin-left: 20px"
          v-model="endDate"
          type="date"
          placeholder="结束日期"
          value-format="YYYY-MM-DD"
        />

        <el-button type="primary" style="margin-left: 20px" @click="searchFun"
          >查询</el-button
        >
        <el-button @click="resetFun">重置</el-button>
        <el-button type="success" @click="downFun"
          ><el-icon> <IEpDownload /> </el-icon>导出</el-button
        >
      </div>
      <div class="right">
        <div style="margin-right: 20px">
          <label>合同总数：</label>
          <span> {{ statisticsInfo.num }} </span>
        </div>
        <div>
          <label>合同总金额：</label>
          <span> ￥{{ statisticsInfo.parice }} </span>
        </div>
      </div>
      <!-- <el-button v-if="showItem" class="right" type="primary" @click="addFun" size="small">
                + 添加
            </el-button> -->
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%; margin-top: 15px"
      :height="store.tableHeight - 50"
    >
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="order_no" label="合同编号" />
      <el-table-column prop="order_name" label="合同名称" />
      <el-table-column prop="customer_name" label="客户名称" />
      <el-table-column prop="amount_real" label="合同金额(元)" />
      <el-table-column
        prop="order_date"
        label="签署日期"
        show-overflow-tooltip
      />
      <el-table-column prop="pay_amount" label="已收款" />
      <el-table-column prop="" label="发货状态">
        <template #default="{ row }">
          <el-tag v-if="row.order_status == 3">待发货</el-tag>
          <el-tag v-else type="success">已发货</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="a" label="合同状态">
        <template #default="{ row }">
          <el-tag v-if="row.status == 1">实施中</el-tag>
          <el-tag v-else-if="row.status == 2" type="warning">已完结</el-tag>
          <el-tag v-else-if="row.status == 3" type="success">已付全款</el-tag>
          <el-tag v-else-if="row.status == 0" type="danger">待开始</el-tag>
          <el-tag v-else-if="row.status == -1" type="info">无效</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="owner" label="销售" />
      <el-table-column
        prop="created_at"
        label="录入时间"
        show-overflow-tooltip
      />
      <el-table-column width="120" label="操作" align="center">
        <template #default="{ row }">
          <el-button type="primary" link @click="saveFun(row)">
            查看
          </el-button>
          <el-button
            type="success"
            v-if="row.status == 1 || row.order_status == 3"
            link
            @click="sendFun(row)"
          >
            发货
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px; display: flex; justify-content: center">
      <el-pagination
        :small="true"
        v-model:currentPage="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
  <addDialog
    ref="addDialogRef"
    @refreshList="refreshList"
    :showBtn="false"
    :dataInfo="selectInfo"
  />
  <sendPage ref="sendPageRef" @refreshList="refreshList" />
  <!--  -->
</template>
<script setup>
import { all_listApi as listApi } from "@/plugins/api/customer/manage/tab4";
import { useCounterStore } from "@/stores/counter.js";
import { detailApi } from "@/plugins/api/customer/manage/tab4.js";
import { ordersExportApi } from "@/plugins/api/down";
import addDialog from "../customer/manage/components/tabDialog/Tab4-dialog.vue";
import sendPage from "./components/Dialogorder-sale-send.vue";
let route = useRoute();
// 合同信息

const store = useCounterStore();
const props = defineProps({});

let tableData = ref([]);
// 分页
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
const handleSizeChange = (val) => {
  pageSize.value = val;
  getData();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  getData();
};
let statisticsInfo = ref({
  num: 0,
  parice: 0,
});
function searchFun() {
  currentPage.value = 1;

  tableData.value = [];
  getData();
}
function resetFun() {
  currentPage.value = 1;
  name.value = "";
  order_status.value = "";
  startDate.value = "";
  endDate.value = "";
  tableData.value = [];
  customer_name.value = "";
  getData();
}
let name = ref("");
let order_status = ref("");
let startDate = ref("");
let endDate = ref("");
let customer_name = ref("");
function getData() {
  listApi({
    page: currentPage.value,
    pageSize: pageSize.value,
    name: name.value,
    order_status: order_status.value,
    start_date: startDate.value,
    end_date: endDate.value,
    customer_name: customer_name.value,
  }).then((res) => {
    tableData.value = res.list;
    statisticsInfo.value.num = res.total;
    statisticsInfo.value.parice = res.total_money;
    total.value = res.page.total;
  });
}
function refreshList() {
  getData();
}
let addDialogRef = ref(null);
function addFun() {
  addDialogRef.value.initFun("add");
}
let selectInfo = ref({});
function detailFun(id) {
  return new Promise((resolve, reject) => {
    detailApi(id).then((res) => {
      selectInfo.value = { ...res };
      selectInfo.value.name = res.customer_name;

      resolve(res);
    });
  });
}
function saveFun(row) {
  detailFun(row.id).then((data) => {
    addDialogRef.value.initFun("update", data);
  });
  //
}
function initFun() {
  tableData.value = [];
  getData();
}
defineExpose({
  initFun,
});
// 发货
let sendPageRef = ref(null);
function sendFun(row) {
  sendPageRef.value.initFun(row);
}
// 导出

function downFun() {
  ordersExportApi({
    name: name.value,
    order_status: order_status.value,
    start_date: startDate.value,
    end_date: endDate.value,
    customer_name: customer_name.value,
  }).then((res) => {
    const url = window.URL.createObjectURL(new Blob([res]));
    const link = document.createElement("a");
    link.href = url;
    link.setAttribute("download", "销售订单.xlsx");
    document.body.appendChild(link);
    link.click();
  });
}
//
onMounted(() => {
  let date = route.query.date;
  if (date) {
    startDate.value = date;
  }
  getData();
});
</script>

<style lang="scss" scoped>
.custom-tab-page {
  width: 100%;

  .tab4-title {
    display: flex;
    justify-content: space-between;
    height: 46px;
    background: #fafbfc;
    box-sizing: border-box;
    padding-left: 10px;

    > div {
      align-self: center;
      margin-right: 20px;
    }

    .left {
      display: flex;
    }

    .right {
      align-self: center;
      margin-right: 10px;
      display: flex;

      label {
        font-size: 14px;
        font-weight: 500;
        color: #5e6c84;
      }

      span {
        font-size: 14px;
        font-weight: 500;
        color: #005ecc;
      }
    }
  }
}
</style>
